package com.git.contractmanagement.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.git.contractmanagement.common.Result;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * sa-Token - 未登录
     */
    @ExceptionHandler(NotLoginException.class)
    public Result handleNotLogin(NotLoginException e) {
        return Result.error(401,"未登录访问：" + e.getMessage());
    }

    /**
     * sa-Token - 无角色
     */
    @ExceptionHandler(NotRoleException.class)
    public Result handleNotRole(NotRoleException e) {
        return Result.error(403,"缺少角色：" + e.getRole() );
    }

    /**
     * sa-Token - 无权限
     */
    @ExceptionHandler(NotPermissionException.class)
    public Result handleNotPermission(NotPermissionException e) {
        return Result.error(403,"缺少权限：" + e.getCode() );
    }

    /**
     * HTTP 方法不支持（如 GET/POST 之外的错误转发）
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Result handleMethodNotAllowed(HttpRequestMethodNotSupportedException e) {
        return Result.error(405, "方法不支持: " + e.getMethod());
    }

    /**
     * 兜底异常处理
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        e.printStackTrace();
        return Result.error(500, "服务器内部错误");
    }
}
